<%-- 
    Document   : dologin
    Author     : Mike
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="login" class="login.LoginInfo" scope="session"/>
<jsp:setProperty name="login" property="*"/>
<jsp:useBean id="member" class="login.userDBDriver" scope="session"/>

<%!
public String validateInputs(String inputusername, String inputpassword, boolean inputexist)
{
    String errormessage = "none";
    try{
            if(inputusername.equals("") || inputpassword.equals("") || inputusername == null || inputpassword == null)
            {
                errormessage = "Login failed. Cant be blank.";
            }
            else if(inputexist == false)
            {
                errormessage = "Login failed. Username doesn't exist.";
            }
        }
    catch (NullPointerException e)
    {
        errormessage = "Login failed. Cant be blank.";
    }
    return errormessage;
}
%>

<%
    String statusMessageL = "User login successful.";
    int answercat = member.createCategoryDB("userDB");
    int answer1 = member.databaseConnect("userDB");
    String result1 = "";
    String result2 = "";
    boolean error = false;
    boolean existcheck = true;
    String tempUsername = "";
    String tempPassword = "";
    String tempEmail = "";
    String tempRealname = "";
    String currentPage = "";
    String redirectURL = "";
    String[] detailsArray = null;
    String[] outputArray = null;
try{
        tempUsername = login.getUsername();
        tempPassword = login.getPassword();
        currentPage = (String) session.getAttribute("currentPage");
}
catch (NullPointerException e){
        error = true;
        statusMessageL = "Login failed. Cant be blank.";
}

try{
    existcheck = member.checkExists(tempUsername);
    result1 = validateInputs(tempUsername, tempPassword, existcheck);
    if(result1.equals("none"))
    {
       detailsArray = member.getUser(tempUsername);
       outputArray = detailsArray[0].split("\t");
       //Check Password
       if(outputArray[3].equals(tempPassword)){
            statusMessageL = "User login successful.";
            error = false;
       }
       else{
            error = true;
            statusMessageL = "Login failed. Check the password or register below:";
       }
    }
    else
    {
       error = true;
       statusMessageL = "Login failed. Try a different username or register below:";
    }
}
catch (NullPointerException e)
{
    error = true;
    statusMessageL = "Login failed. Cant be blank.";
}

//redirect to registration page

    if(error == true){
       login.setAllNull();
       session.setAttribute( "loggedin_username", null);
       session.setAttribute( "loggedin_email", null);
       session.setAttribute( "loggedin_realname", null);
    }
    else{
        login.setAllNull();
        session.setAttribute( "loggedin_username", outputArray[0]);
        session.setAttribute( "loggedin_email", outputArray[2]);
        session.setAttribute( "loggedin_realname", outputArray[3]);
    }
    session.setAttribute( "statusMessageL", statusMessageL);

    if(statusMessageL.contains("successful") && currentPage != null){
        redirectURL = currentPage;
    }
    else{
        redirectURL = "register.jsp";
    }

    statusMessageL = "";
    result1 = "";
    result2 = "";
    error = false;
    existcheck = true;
    tempUsername = "";
    tempPassword = "";
    tempEmail = "";
    tempRealname = "";

    response.sendRedirect(redirectURL);
%>

